package com.gooluke.user.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;

/**
 * @author gooluke
 * description
 * datetime 2025-03-29 2:38
 */
@Configuration
@EnableWebSecurity
public class SecurityConfig {

    /**
     * 这里测试用，暂时不需要认证
     */
    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http
                .authorizeHttpRequests(authorize -> authorize
                        //.requestMatchers("/api/user/**").permitAll() // 放行用户接口
                        //.anyRequest().authenticated()               // 其他接口需要认证
                        .anyRequest().permitAll() // 允许所有请求无需认证
                )
                .csrf(AbstractHttpConfigurer::disable); // 关闭 CSRF

        return http.build();
    }

    /**
     * 配置密码编码器
     *
     * @return PasswordEncoder 密码编码器，这里使用BCrypt进行密码加密
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

}
